package com.netsdk.module;

import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.NetSDKLib.LLong;
import com.netsdk.utils.CoreUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录模块
 * 主要有：初始化、登录、登出
 */
@Slf4j
public class LoginModule {
	public static NetSDKLib netsdk = NetSDKLib.NETSDK_INSTANCE;

	// 登陆句柄
	public static LLong m_hLoginHandle = new LLong(0);   
	
	private static boolean bInit    = false;
	private static boolean bLogopen = false;

	public static Map<String, LLong> loginMap = new HashMap<>();

	// 初始化sdk
	public static boolean init(NetSDKLib.fDisConnect disConnect, NetSDKLib.fHaveReConnect haveReConnect) {
		// 初始化返回值
		bInit = netsdk.CLIENT_Init(disConnect, null);
		// 判断初始化是否成功
		if (!bInit) {
			log.error("初始化失败!!!");
			return false;
		}

		//打开日志
		NetSDKLib.LOG_SET_PRINT_INFO setLog = new NetSDKLib.LOG_SET_PRINT_INFO();
		File path = new File("../sdk.log");
		if (!path.exists()) {
			path.mkdir();
		}
		String logPath = CoreUtil.getRoot() + File.separator + "sdk_log" + File.separator;
		setLog.bSetFilePath = 1;
		setLog.nPrintStrategy = 0;
		System.arraycopy(logPath.getBytes(), 0, setLog.szLogFilePath, 0, logPath.getBytes().length);

		log.info("日志路径: {}", logPath);

		setLog.bSetPrintStrategy = 1;
		bLogopen = netsdk.CLIENT_LogOpen(setLog);
		if (!bLogopen) {
			log.error("SDK日志打印失败!!!");
		}

		// 设备断线，SDK自动重连
		netsdk.CLIENT_SetAutoReconnect(haveReConnect, null);

		// ------------- 设置登录超时时间和尝试次数，可选 -------------
		// 响应超时时间
		int waitTime = 5000;
		// 尝试连接1次
		int tryTimes = 1;
		netsdk.CLIENT_SetConnectTime(waitTime, tryTimes);

		// ------------- 设置更多网络参数，NET_PARAM的nWaittime，nConnectTryNum成员与CLIENT_SetConnectTime -------------
		// 接口设置的登录设备超时时间和尝试次数意义相同,可选
		NetSDKLib.NET_PARAM netParam = new NetSDKLib.NET_PARAM();
		// 响应超时时间
		netParam.nConnectTime = 10000;
		// 子连接超时时间
		netParam.nGetConnInfoTime = 3000;
		// 设备超时时间，为0默认1000ms
		netParam.nGetDevInfoTime = 3000;
		netsdk.CLIENT_SetNetworkParam(netParam);

		return true;
	}
	
	/**
	 * 清除环境
	 */
	public static void cleanup() {
		if (bLogopen) {
			netsdk.CLIENT_LogClose();
		}
		if (bInit) {
			netsdk.CLIENT_Cleanup();
		}
	}

	// 登录设备
	public static LLong login(String m_strIp, int m_port, String m_strUser, String m_strPassword) {
		// 入参
		NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY pstInParam = new NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY();
		pstInParam.emSpecCap = 0;
		pstInParam.nPort = m_port;
		pstInParam.szIP = m_strIp.getBytes();
		pstInParam.szUserName = m_strUser.getBytes();
		pstInParam.szPassword = m_strPassword.getBytes();

		// 出参
		NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY pstOutParam = new NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY();
		// 高安全级别登录
		m_hLoginHandle = netsdk.CLIENT_LoginWithHighLevelSecurity(pstInParam, pstOutParam);

		// 保存本地设备登陆id
		loginMap.put(m_strIp, m_hLoginHandle);

		return m_hLoginHandle;
	}

	/**
	 * 登出设备
	 * @param m_hLoginHandle  登陆句柄
	 */
	public static boolean logout(LLong m_hLoginHandle) {
		boolean bRet = false;
		if (m_hLoginHandle.longValue() != 0) {
			bRet = netsdk.CLIENT_Logout(m_hLoginHandle);
			m_hLoginHandle.setValue(0);
		}
		return bRet;
	}
}
